<?php
/* 
 * Functions for handling HTTP GET requests.
 */

require_once('environment.php');
require_once('lib.php');
require_once('misc.php');
require_once('html.php');
require_once('data_access.php');
require_once('simulation.php');
require_once('paginator.php');

/**
 * Print HTML form for creating new investment suggestion.
 */
function http_get_upcoming_new() {
    print_html_head(HEAD_TAB_UPCOMING);
    print_html_investment_form();
    print_html_foot();
}

/**
 * Print HTML article showing full information about investment (live,
 * upcoming and proposed).
 * @param array request regex arguments
 */
function http_get_investment($investment_id) {

    // read investment data, from either live or upcoming database tables
    if (!$investment = read_investment($investment_id)) {
        http_not_found();
    }

    // print page HTML
    print_html_head();
    print_html_investment_article($investment);
    print_html_comment_form($investment_id);
    $paginator = get_paginator_for_comments();
    $paginator->print_page(false, $investment_id . ',');
    print_html_foot();
}

/**
 * Display the standard page showing the current month's candidates and all
 * prior investments.
 */
function http_get_history() {
    global $_CONFIG;
    
    // read current month's candidates (in random order to avoid bias)
    $this_month = read_this_month();
    $current_candidates = read_candidates($this_month);
    if ($current_candidates) {
        shuffle($current_candidates);
    }

    /* if there are completed investments read the votes received by each of
     * last month's candidates, and shift the most recent investment from the
     * investments array (to be rendered differently) */
    if ($this_month > strtotime($_CONFIG->investment_start)) {
        
        // ***
        
        $previous_investment_id = read_winning_candidate_id(subtract_month($this_month));
        
        // ***

        $previous_investment = read_investment($previous_investment_id);
        $previous_candidates = read_candidates(subtract_month($this_month));
        foreach ($previous_candidates as $value) {

            // read votes
            $votes = read_votes(subtract_month($this_month));

            $previous_votes[] = array(
                'title' => $value['title'],
                'votes' => count($votes[$value['id']]),
                'record_id' => $value['id']);
        }
    }

    // print page HTML
    print_html_head(HEAD_TAB_BLOG);
    print_candidates($current_candidates);
    if (isset($previous_candidates)) {
        print_html_investment_short($previous_investment, $previous_votes);
    } else {
        print_html_no_investment_shorts(NO_INVESTMENT_SHORTS_COMPLETE);
    }
    $paginator = get_paginator_for_complete_investments();
    $first_page_id = subtract_month(read_this_month(), 2);
    $paginator->print_page(false, $first_page_id);
    print_html_foot();
}

/**
 * Return single page of investment summaries (as an extension to the standard
 * page).
 */
function http_get_more_investments() {
    $paginator = get_paginator_for_complete_investments();
    $paginator->print_page(true);
}

/**
 * Show first page of list of upcoming investment candidates, along with button
 * allowing users to suggest investments.
 */
function http_get_upcoming() {
    print_html_head(HEAD_TAB_UPCOMING);
    print_html_upcoming_head();
    $paginator = get_paginator_for_candidate_investments();
    $paginator->print_page(false, 0);
    print_html_foot();
}

/**
 * Expand list of upcoming investment candidates.
 */
function http_get_more_upcoming() {
    $paginator = get_paginator_for_candidate_investments();
    $paginator->print_page(true);
}

/**
 * Expand list of comments beloning to investment.
 */
function http_get_more_comments() {
    $paginator = get_paginator_for_comments();
    $paginator->print_page(true);
}

/**
 * Return avatar associated with a user as PNG image.
 * @param string username
 */
function http_get_avatar($username) {
    global $_CONFIG;

    // count number of avatar images in avatar simulation folder
    $dir = new DirectoryIterator($_CONFIG->simulation_avatar_dir);
    foreach($dir as $fileinfo) {
        if ($fileinfo->isFile()) {
            $avatars[] = $fileinfo->getPathname();
        }
    }

    // pick random avatar
    $file_name = array_rand_value($avatars);

    $img = imagecreatefrompng($file_name);
    $THUMB_SIZE = 32;
    $img = thumbnail($img, $THUMB_SIZE);

    // stream thumbnail image to client
    header("Content-type: image/png");
    imagepng($img);
    imagedestroy($img);
}

/**
 * Stream PNG image associated with an investment.
 * @param string Investment image record ID.
 */
function http_get_investment_img_full($investment_id) {
    stream_cachable_img($investment_id);
}

/**
 * Stream thumbnail PNG image associated with an investment.
 * @param string Investment image record ID.
 */
function http_get_investment_img_thumb($investment_id) {
    stream_cachable_img($investment_id, '_80', function(&$img) {
            $THUMB_SIZE = 80;
            // original image resource destroyed by thumbnail()
            $img = thumbnail($img, $THUMB_SIZE);
        });
}

/**
 * Print simulation control panel.
 */
function http_get_sim() {
    print_html_head();
    print_html_sim();
    print_html_foot();
}

?>
